using UnityEngine;
using System.Collections;

[RequireComponent (typeof (MeshCollider))]
public class UIButton : MonoBehaviour {
	
	private UIState uiState;
	public StateTexture [] stateTextures;
	private MouseEvent mouseEvent;
	virtual public void Awake () {
		mouseEvent = MouseEvent.none;
		uiState = UIState.normal;
		ChangeTexture();
	}
	
	#region MouseEvent
	void OnMouseExit()
	{
		LeaveButton();
	}
	void OnMouseOver()
	{
		HoverButton();
	}
	
	public void OnMouseDown()
	{
		PressButton();
	}
	
	void OnMouseUp()
	{
		ReleaseButton();
	}
	#endregion
	
	#region OwnHandler
	virtual public void HoverButton()
	{
		if(mouseEvent != MouseEvent.press){
			mouseEvent = MouseEvent.over;
			uiState = UIState.hover;
			ChangeTexture();
		}
	}
	
	virtual public void PressButton()
	{
		mouseEvent = MouseEvent.press;
		uiState = UIState.active;
		ChangeTexture();
	}
	
	virtual public void ReleaseButton()
	{
		mouseEvent = MouseEvent.up;
		uiState = UIState.normal;
		ChangeTexture();
	}
	
	virtual public void LeaveButton()
	{
		mouseEvent = MouseEvent.none;
		uiState = UIState.normal;
		ChangeTexture();
	}
	#endregion
	
	private void ChangeTexture()
	{
		renderer.material.mainTexture = stateTextures[(int)uiState].texture;
	}
	
	
	
}
